function createObject(){
    var request_type;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
        request_type = new ActiveXObject("Microsoft.XMLHTTP");
    else
        request_type = new XMLHttpRequest();

    return request_type;
}

var http = createObject();

var nocache = 0;

function login() {
    var passwd = encodeURI(document.getElementById('passwd').value);
    var email = encodeURI(document.getElementById('login').value);
    var request;
    var nocache = Math.random();

    $("#dbError").attr("style","display: none;");
    $("#loginError").attr("style","display: none;");

    request = 'act=checkUser&email='+email+'&passwd='+passwd+'&nocache='+nocache;
    http.open('post', 'index.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = function() {
        if(http.readyState == 4) {
        var response = http.responseText;
        if (response != "0") {
            $('#enterPl').fadeOut('slow',function(){$('#quitPl').fadeIn('slow');});
            $('#userInfo').fadeIn('slow');
        } 
    }
    };
    http.send(request);
}

function exit() {
    request = 'act=exitUser&nocache='+nocache;
    http.open('post', 'index.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = function() {
        if(http.readyState == 4) {
        var response = http.responseText;
        if (response != "0") {
            $('#quitPl').fadeOut('slow',function(){$('#enterPl').fadeIn('slow');});
            $('#userInfo').fadeOut('slow');
        } 
    }
    };
    http.send(request);
}

function sendQuestion()
{
    var name = encodeURI(document.getElementById('Qname').value);
    var messa = encodeURI(document.getElementById('Qmessa').value);
    var theme = encodeURI(document.getElementById('Qtheme').value);
    var email = encodeURI(document.getElementById('Qemail').value);
    var capcha = encodeURI(document.getElementById('Q_tt_pass').value);
    nocache = Math.random();
    $('#showMailUser').html(email);

    var request = 'email='+email+'&capcha='+capcha+'&messa='+messa+'&theme='+theme+'&name='+name+'&nocache='+nocache;
    http.open('post', '/scripts/QuestionToMan.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = sendReply;
    http.send(request);
}



function sendReply()
{
    if(http.readyState == 4)
    {
        var response = http.responseText;
        if (response == "1"){
            $("#askForm").attr('style','display: none;');
            $("#sent-system-fail").attr('style','display: none;');
            $("#sent-user-fail").attr('style','display: none;');
            $("#successfully-sent").attr('style','');
        }
        if (response == "-1") {
            $("#askForm").attr('style','display: none;');
            $("#sent-system-fail").attr('style','');
        }
        if (response == "-2") {
//            $("#askForm").attr('style','display: none;');
            $("#sent-user-fail").attr('style','');
        }
        if (response == "-8") {alert ("Неверное контрольное слово. Введите еще раз, то что вы видите на картинке.");
                               var pic=document.getElementById("cap-pic");
                               pic.src="../scripts/capcha_image.php";}
        if (response == "-9") alert ("Вы не ввели слово с картинки!");


    }

 }

function exitReply()
{
    if(http.readyState == 4)
    {
        var response = http.responseText;
        if (response == "1") location.replace ("../../index.php");
    }

 }

function exitClient()
{
    http.open('get', '../exit.php');
    http.onreadystatechange = exitClientReply;
    http.send(null);
}

function exitClientReply()
{
    if(http.readyState == 4)
    {
        var response = http.responseText;
        if (response == "1") location.replace ("../../../index.php");
    }
 }

 function exitMn(who)
{
    http.open('get', '/scripts/exit.php?who='+who);
    http.onreadystatechange = exitMnReply;
    http.send(null);
}

function exitMnReply()
{
    if(http.readyState == 4)
    {
        if (http.responseText) location.replace ("index.php");
    }
 }

 function formfocus()
 {
    document.getElementById('email').focus();
 }

 function disableLight()
 {
     $('#hrDownload').attr('style','');
 }

 function changePassword ()
 {
    var request;
    request = 'email='+encodeURI($('#emailForPass').val());
    
    http.open('post', '/scripts/createNewPasswd.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = function(){
            if(http.readyState == 4){
                alert(http.responseText);
        }
    };
    http.send(request);
 }